/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

/**
 * @author Eduardo R Danielli
 *
 * @param <T> Objects type
 * @param <R> Return type
 */
public abstract class MultipleObjectsCommand<T, R> implements Command<R> {
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	private Collection<T> objectList;

	public MultipleObjectsCommand(final Collection<T> objects) {
		setObjectList(objects);
	}
	
	public MultipleObjectsCommand(final T ... objects) {
		setObjectList(objects);
	}
	
	protected final void setObjectList(final T... objectList) {
		Collection<T> validList = null;
		boolean isNullOrEmptyAllowed = isNullOrEmptyAllowed();
		
		if (logger.isDebugEnabled()) {
			logger.debug("isNullOrEmptyAllowed: " + isNullOrEmptyAllowed);
		}
		
		if (isNullOrEmptyAllowed) {
			
			if (objectList == null || objectList.length == 0) {
				logger.debug("Provided objectList array is null or empty. Using default");
				validList = Collections.emptyList();
				
			} else {
				logger.debug("Using provided objectList array as java.util.List");
				validList = Arrays.asList(objectList);
			}
			
		} else {
			Assert.notNull(objectList, "objectList must not be null, as defined by nullOrEmptyAllowed()");
			Assert.notEmpty(objectList, "At least one object is required, as defined by nullOrEmptyAllowed()");
			
			logger.debug("Using provided objectList array as java.util.List");
			validList = Arrays.asList(objectList);
		}

		this.objectList = validList;
	}

	protected final void setObjectList(final Collection<T> objectList) {
		Collection<T> validList = null;
		boolean isNullOrEmptyAllowed = isNullOrEmptyAllowed();
		
		if (logger.isDebugEnabled()) {
			logger.debug("isNullOrEmptyAllowed: " + isNullOrEmptyAllowed);
		}
		
		if (isNullOrEmptyAllowed) {
			
			if (objectList == null) {
				logger.debug("Provided objectList collection is null. Using default");
				validList = Collections.emptyList();
				
			} else {
				logger.debug("Using provided objectList collection");
				validList = objectList;
			}
			
		}
		
		else {
			Assert.notNull(objectList, "objectList must not be null, as defined by nullOrEmptyAllowed()");
			Assert.notEmpty(objectList, "At least one object is required, as defined by nullOrEmptyAllowed()");
			
			logger.debug("Using provided objectList collection");
			validList = objectList;
		}
		
		this.objectList = validList;
	}

	protected final Collection<T> getObjectList() {
		return objectList;
	}

	protected boolean isNullOrEmptyAllowed() {
		return true;
	}
	
}
